package com.mall.interceptor;

import com.mall.bean.SysUser;
import com.mall.bean.SystemLogs;
import com.mall.bean.User;
import com.mall.service.SystemLogsService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

/**
 * @PageName com.mall.interceptor
 * @Description 日志aop拦截器
 * @Author 何全裕
 * @Create 2017-09-11 21:51
 * @Version 1.0
 **/
@Component
@Aspect
public class LogsAspect {
    @Autowired
    private SystemLogsService systemLogsService;

    //操作日志增加
    @Around("execution(* com.mall.controller..*(..))&&@annotation(log))")
    public Object getLog(ProceedingJoinPoint joinPoint, Log log) throws Throwable{
        long begin = System.currentTimeMillis();
        Object proceed = joinPoint.proceed();
        long end = System.currentTimeMillis();
        Object[] args = joinPoint.getArgs();
        HttpServletRequest request = null;
        for(Object o : args){
            if(o instanceof HttpServletRequest){
                request = (HttpServletRequest) o;
                break;
            }
        }
        if(request != null){
            SystemLogs logs = new SystemLogs();
            SysUser user = (SysUser)request.getSession().getAttribute("user");
            logs.setId(UUID.randomUUID().toString());
            logs.setAvailable("1");
            logs.setDateline(new Date());
            logs.setLogDescribe(log.describe());
            logs.setSysModule(log.model());
            logs.setUser(user.getRealName());
            logs.setUserid(user.getUsername());
            Double respontime = Double.valueOf(end - begin);
            logs.setResponseTime(respontime);
            systemLogsService.insertLogs(logs);
        }
        return proceed;
    }

    @AfterThrowing("execution(* com.mall.controller..*(..))&&@annotation(log))")
    public void getErrorLog(JoinPoint joinPoint, Log log) throws Throwable{
        Object[] methodsObjects = joinPoint.getArgs();
        HttpServletRequest request = null;
        for(Object o:methodsObjects){
            if(o instanceof HttpServletRequest){
                request = (HttpServletRequest) o;
                break;
            }
        }
        if(request!=null) {
            SystemLogs logs = new SystemLogs();
            SysUser user = (SysUser) request.getSession().getAttribute("user");
            if (user == null) {
                logs.setId(UUID.randomUUID().toString());
                logs.setAvailable("0");
                logs.setDateline(new Date());
                logs.setLogDescribe("用户登录失败");
                logs.setSysModule("用户登录");
                systemLogsService.insertLogs(logs);
            } else {
                logs.setId(UUID.randomUUID().toString());
                logs.setAvailable("0");
                logs.setDateline(new Date());
                logs.setLogDescribe(log.describe());
                logs.setSysModule(log.model());
                logs.setUser(user.getRealName());
                logs.setUserid(user.getUsername());
                systemLogsService.insertLogs(logs);
            }
        }
    }
}
